package service

import (
	"context"
	"errors"
	"go-websocket/internal/dao"
	"go-websocket/internal/model/entity"
	"go-websocket/utility"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type userService struct {

}


var User = new(userService)

type user struct {
	*entity.TxzhUser // 匿名嵌套原始实体
	face            string
	photo           string
}

func (s *userService) GetUserInfo (userId int) (userInfo user, err error) {

	userModel := dao.TxzhUser.Ctx(context.TODO())
	err = userModel.Where("id", userId).Scan(&userInfo)

	if err != nil {
		return userInfo, err
	}

	var chatState = bson.M{}
	conn := utility.MgConn("default")
	chatStateColl := conn.GetColl("txzh_user_state")
	err = chatStateColl.FindOne(context.TODO(), bson.M{"user_id": userId}, options.FindOne().SetProjection(bson.M{"photo": 1})).Decode(&chatState)

	if err != nil {
		// 检查是否是没有找到数据的情况
		if !errors.Is(err, mongo.ErrNoDocuments) {
			return userInfo, err
		}

	}

	facePath := utility.GetShowPhoto(chatState, userInfo.Sex, userId, 300)

	userInfo.face = facePath

	userInfo.photo = facePath

	return userInfo, nil
}




func (s *userService) Base (userId interface{}) error {

	userState := map[string]interface{}{
		"no_reader_circle": 0,
		"no_reader_circle_chat_num": 0,
		"clear_circle_data": 0,
	}
	circleImg := "default_circle_img.jpg"
	photo := ""

	//mongo
	conn := utility.MgConn("default")
	userStateColl := conn.GetColl("txzh_user_state")

	var userStateObj = bson.M{}
	err := userStateColl.FindOne(context.TODO(), bson.M{"user_id": userId}).Decode(&userStateObj)


	if err != nil {
		return err
	}

	if len(userStateObj) != 0 {
		userState["no_reader_circle"] = userStateObj["reader_circle"]
		isNum := 0
	}

}